import req from "./req.js";
import axios from "./index";
import _orderBy from "lodash/orderBy";

import { champDict } from "@/constant/lolDate";
import { flatRunes } from "@/constant/runes";

function isValKey(
  key: string | number | symbol,
  object: object
): key is keyof typeof object {
  return key in object;
}

// 获取当前正在选择的英雄
// 可能存在bug
// ata.myTeam[0].championId 可能判断选择英雄id 不准确
export const heroOfChoice: (
  summonerId: number
) => Promise<
  { champId: string; label: string; alias: string; title: string } | undefined
> = async (summonerId: number) => {
  let data = (await axios.get("/lol-champ-select/v1/session")) as any;
  if (data.httpStatus !== 404 && data.myTeam) {
    let myTeam = data.myTeam.filter((x: any) => x.summonerId === summonerId)[0];
    let id = myTeam.championId;

    if (isValKey(id, champDict)) {
      let hero = champDict[id];
      return hero;
    }
  } else {
    return undefined;
  }
};

// 获取当前符文信息
/**
 * 
 * @returns {
    "autoModifiedSelections": [],
    "current": true,
    "id": 728312528,
    "isActive": true,
    "isDeletable": true,
    "isEditable": true,
    "isTemporary": false,
    "isValid": true,
    "lastModified": 1667805227764,
    "name": "Blitz: 残月之肃",
    "order": 1,
    "primaryStyleId": 8000,
    "selectedPerkIds": [
        8008,
        8009,
        9103,
        8014,
        8304,
        8345,
        5005,
        5008,
        5002
    ],
    "subStyleId": 8300
}
 */
export const getCurrentRunePage: () => Promise<{
  // 副系id
  subStyleId: number;
  // 主系id
  primaryStyleId: number;
  // 所有符文
  selectedPerkIds: number;
  // 符文页名称
  name: string;
  // 符文页id
  id: number;
}> = async () => {
  let data = (await axios.get("/lol-perks/v1/pages")) as any;
  for (let i = 0; i < data.length; i++) {
    if (data[i].current) {
      return data[i];
    }
  }

  return undefined;
};

// 删除符文
export const deleteCurrentRunePage = async (id: number) => {
  let data = await axios.delete(`lol-perks/v1/pages/${id}`);
  return data;
};

// 写入符文
export const setCurrentRunePage = async (rune: any) => {
  let data = await axios.post("/lol-perks/v1/pages", rune);
  return data;
};

// 获取 当前符文页信息
let getFuWenYe = async () => {
  let data = (await axios.get("/lol-perks/v1/pages")) as any;
  for (let i = 0; i < data.length; i++) {
    if (data[i].current) {
      return data[i];
    }
  }
};
